<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" class="avatar" />
          用户名：{{userInfo.nickName}}
        </li>
        <li>我的积分：{{userInfo.coin}}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn"  v-if="isLogined">
   <img src="../assets/img/cart.png" alt="" />
   <span>购物车</span>
   <strong>{{ cartTotal }}</strong>
</li>
        <li class="login-btn" v-else @click="changIsShow(true)">登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import {mapMutations,mapState,mapActions} from 'vuex'
import {WeixinLoginApi} from '@/request/api'
export default {
  data () {
    return {
      //  cartTotal:0,
    }
  },
  computed:{
        ...mapState({
            isLogined:state=>state.loginStatus.isLogined,
            cartTotal: (state) => state.userInfo.cartTotal,
            userInfo: (state) => state.userInfo.userInfo,
        })
        
    },
  methods: {
    ...mapMutations({
        changIsShow:'showModal/changIsShow',
         chanIsLogined:"loginStatus/chanIsLogined",
         initUserInfo:'userInfo/initUserInfo'
    }),
     ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
      changeUserInfo:"userInfo/changeUserInfo",
      asyncChangeUserInfo:'userInfo/asyncChangeUserInfo'
    }),
},
created () {
    // console.log('topbar-created');
    // 获取地址栏中的code值
    console.log('code',this.$route.query.code);
    this.$router.onReady(async()=>{
    console.log('ready-code',this.$route.query.code);
    let pathCode=this.$route.query.code;
    if(pathCode){
      let res =await WeixinLoginApi({
        code:pathCode
      });
      console.log('****res',res);
      if(res.code==0){
          //  // 1.提示登陆成功
            alert('微信扫码成功');
            //2.存储token，后面发请求时需要用到。localstorage setItem() getItem()
            localStorage.setItem('x-auth-token',res['x-auth-token']);
            // 3.隐藏登录框
            // this.changeIsShowLoginModal(false);
            // 购物车切换
            this.chanIsLogined(true)
            this.$router.push(this.$route.path)
            this.asyncChangeUserInfo()
      }else if(res.code==400){
        // alert('请重新扫码')
         this.asyncShowToast({
          isShow: true,
          title: "请重新扫码！",
          type: "danger",
        });
        this.changIsShow(true);
      }else if(res.code==407){
        this.asyncShowToast({
          isShow: true,
          title: "请再进行一次手机号登陆！",
          type: "danger",
        });
        // alert('手机登陆')
        // 2自动打开登陆框
        this.changIsShow(true);
        // 3.存uuid  为了login.vue时再使用 进行关联
        localStorage.setItem('uuid',res.uuid);
        // 4.清除地址栏中的code
        this.$router.push(this.$route.path)
      }
    }else{
            console.log('非扫码登陆,普通登陆页面切换');

        let token =localStorage.getItem('x-auth-token');
        this.chanIsLogined(Boolean(token))
        if(token){
          this.asyncChangeUserInfo()
        }else{
          this.initUserInfo();
        }
    }
    })
  },
  // watch: {
  //   "$route.path":{
  //     handler(newVal,oldVal){
  //       console.log('地址改变了',newVal,oldVal);

  //       let token =localStorage.getItem('x-auth-token');
  //       this.chanIsLogined(Boolean(token))
  //     }
  //   }
  // }
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    .right {
      li {
        float: left;
        margin-left: 20px;
        text-align: center;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          background: #0a328e;
        }
         &.cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center; 
        img{
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span{
          margin: 0 8px;
        }
        strong{
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
      }
     
      .avatar {
        width: 23px;
        height: 23px;
        border-radius: 50%;
        vertical-align: middle;
      }
    }
  }
}
</style>